package com.atguigu.flink.cep;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.List;
import java.util.Map;

/**
 * Created by Smexy on 2023/3/3
 *
 *  模式序列指多个不同的模式，使用某种关系进行连接
 *
 *      模式1 紧挨着  模式2
 *      模式1 不紧挨着  模式2
 *        ....
 */
public class Demo5_PatternSequence
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            .<WaterSensor>forMonotonousTimestamps()
            .withTimestampAssigner((e, r) -> e.getTs());


        SingleOutputStreamOperator<WaterSensor> ds =
            env.readTextFile("data/cep.txt")
               .map(new WaterSensorMapFunction())
               .assignTimestampsAndWatermarks(watermarkStrategy);

       /* Pattern<WaterSensor, WaterSensor> s2 = Pattern.<WaterSensor>begin("s2")
            .where(new SimpleCondition<WaterSensor>()
            {
                @Override
                public boolean filter(WaterSensor value) throws Exception {
                    return "s2".equals(value.getId());
                }
            });
*/

        Pattern<WaterSensor, WaterSensor> pattern = Pattern.<WaterSensor>begin("s1")
            .where(new SimpleCondition<WaterSensor>()
            {
                @Override
                public boolean filter(WaterSensor value) throws Exception {
                    return "s1".equals(value.getId());
                }
            })
            //当前数据必须是s1，和当前数据紧挨着的下一条数据，必须是s2，如果符合这个规则，把s1和s2都匹配到输出
            //.next("s2")
            //当前数据必须是s1，和当前数据紧挨着的下一条数据，不能是s2，如果符合这个规则，把s1输出
            //.notNext("s2")
            //当前数据必须是s1，如果之后的有s2，那么就和这条s2匹配。把s1，s2输出
            //.followedBy("s2")
            //当前数据必须是s1，只要之后有s2，那么就和s2匹配。把s1，s2输出
            //.followedByAny("s2")
            //当前数据必须是s1，且当前数据之后不能有s2，符合条件就输出 s1
            //NotFollowedBy is not supported as a last part of a Pattern!  伪条件，不成立的条件
            // 当前数据必须是s1，当它匹配到后面的第一个s3时，如果之间没有s2,符合条件就输出 s1,s3
            .notFollowedBy("s2")
            .where(new SimpleCondition<WaterSensor>()
            {
                @Override
                public boolean filter(WaterSensor value) throws Exception {
                    return "s2".equals(value.getId());
                }
            })
            .followedBy("s3")
            .where(new SimpleCondition<WaterSensor>()
            {
                @Override
                public boolean filter(WaterSensor value) throws Exception {
                    return "s3".equals(value.getId());
                }
            });


        PatternStream<WaterSensor> patternStream = CEP.pattern(ds, pattern);


        patternStream.select(new PatternSelectFunction<WaterSensor, String>()
        {

            @Override
            public String select(Map<String, List<WaterSensor>> map) throws Exception {
                return map.toString();
            }
        })
                     .print().setParallelism(1);


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
